#include "welcomewindow.h"
#include "ui_welcomewindow.h"
#include<QJsonArray>
#include<QMovie>
#include<QTimer>
#include "staffwindow.h"
#include "managewindow.h"
welcomeWindow::welcomeWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::welcomeWindow)
{
    ui->setupUi(this);
    Manager=new QNetworkAccessManager;

    connect(Manager,&QNetworkAccessManager::finished,this,&welcomeWindow::getData);
    QNetworkRequest request(QUrl("http://t.weather.itboy.net/api/weather/city/101280101"));
    Manager->get(request);

    QTimer::singleShot(5000,this,SLOT(jumpNext()));

}

welcomeWindow::~welcomeWindow()
{
    delete ui;
}

void welcomeWindow::setnotice(const QString &name)
{
    ui->label->setText("欢迎"+name+"*进入系统");
}

QString &welcomeWindow::getName()
{
    return (ui->label->text().split("*")[2]);
}


void welcomeWindow::getData(QNetworkReply *reply)
{
    QByteArray data=reply->readAll();

//    qDebug()<<data;
    //第三步：将从文件中读取的数据转换成 QJsonDocument
    QJsonDocument doc= QJsonDocument::fromJson(data);
    //第四步：一定要根据json数据格式来进行解析
    QJsonObject object1= doc.object();
    QJsonValue value1=object1.value("cityInfo");

    QJsonObject object2= value1.toObject();

    QJsonValue value2=object1.value("data");

    QJsonObject object3= value2.toObject();


    QJsonArray arr1= object3.value("forecast").toArray();

    ui->textBrowser->append("日期: "+arr1[0].toObject().value("ymd").toString()+"号"+"  "+arr1[0].toObject().value("week").toString());
    ui->textBrowser->append("天气: "+arr1[0].toObject().value("type").toString());
    ui->textBrowser->append("最高温度: "+arr1[0].toObject().value("high").toString());
    ui->textBrowser->append("最低温度: "+arr1[0].toObject().value("low").toString());
    ui->textBrowser->append("日出时间: "+arr1[0].toObject().value("sunrise").toString());
    ui->textBrowser->append("日落时间: "+arr1[0].toObject().value("sunset").toString());
    ui->textBrowser->append("AQI: "+QString::number(arr1[0].toObject().value("aqi").toInt(),10));
    ui->textBrowser->append("风向: "+arr1[0].toObject().value("fx").toString()+"  "+arr1[0].toObject().value("fl").toString());
    ui->textBrowser->append("小贴士: "+arr1[0].toObject().value("notice").toString());

    ui->couragelabel->setText("亲爱的家人，今天也要加油哦~");


    if(arr1[0].toObject().value("type").toString()=="晴")
    {
        QMovie *movie=new QMovie(":/weatheIcon/weatheIcon/sunny.gif");
        ui->weatherCoinlabel->setMovie(movie);
        movie->start();
    }
    else if(arr1[0].toObject().value("type").toString()=="小雨"||arr1[0].toObject().value("type").toString()=="中雨"
            ||arr1[0].toObject().value("type").toString()=="大雨")
    {
        QMovie *movie=new QMovie(":/weatheIcon/weatheIcon/rainy.gif");
        ui->weatherCoinlabel->setMovie(movie);
        movie->start();
    }
    else if(arr1[0].toObject().value("type").toString()=="多云"||arr1[0].toObject().value("type").toString()=="阴")
    {
        QMovie *movie=new QMovie(":/weatheIcon/weatheIcon/couldy.gif");
        ui->weatherCoinlabel->setMovie(movie);
        movie->start();
    }


}

void welcomeWindow::jumpNext()
{
    if(ui->label->text().split("*")[1]=="管理员")
    {
        qDebug()<<"跳转管理员页面";
        manageWindow *m1=new manageWindow;
        m1->show();
        this->hide();

    }
    else
    {
        qDebug()<<"跳转员工页面";
        staffWindow *s1=new staffWindow(ui->label->text().split("*")[2]);
        s1->show();
        this->close();
    }

}
